﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using Microsoft.Phone.Controls;
using Microsoft.Phone.Shell;
using TV_2013.Handle;
using TV_2013.Model;

namespace TV_2013.View.Practice
{
    public partial class PracticeView1 : PhoneApplicationPage
    {
        ReadData readData = new ReadData("Data/DataPractice.xml");
        PracticeModel practiceModel = new PracticeModel();
        private string[] comper;
        int n;

        public PracticeView1()
        {
            InitializeComponent();
            
        }

        // nhan hai tham so la id cua bai + so luong chu
        protected override void OnNavigatedTo(NavigationEventArgs e)
        {
            panelText.Children.Clear();
            string id = NavigationContext.QueryString["id"];
            n = Int32.Parse(NavigationContext.QueryString["n"]);
            practiceModel = readData.GetPracticeDataByAttribute(id, n); // id cua lesson va so luong sound de load
             //truyen vao so luong chu
            comper = new string[n];
            //imgSmaple.Source = new BitmapImage(new Uri(practiceModel.ImgURL, UriKind.Relative));
            string temp = "";
            int j = 0;
            for(int i=0; i<practiceModel.Title.Length; i++)
            {
                temp = temp + practiceModel.Title[i].ToString();
               
                if (practiceModel.Title[i].ToString() == " " || i == practiceModel.Title.Length-1)
                {
                    //luu lai thu tu cac chuoi tach de so sanh
                    comper[j] = temp;
                    Button btntxt = new Button();
                    btntxt.Content = temp;
                    btntxt.Style =  App.Current.Resources["ButtonStyle2"] as Style;
                    btntxt.FontSize = 60;
                    btntxt.Click += btntxt_Click;
                    temp = "";
                    panelText.Children.Add(btntxt);
                    j++;
                }
            }
        }

        void btntxt_Click(object sender, RoutedEventArgs e)
        {
            int j = 0;
            //lay noi dung cua nut so sanh voi nhung phan tu trong comper de biet la click nut nao
            for (int i = 0; i < comper.Length; i++)
            {
                 Button btn = (Button) sender;
                if (btn.Content.ToString()==comper[i])
                {
                    j = i;
                    break;
                }
            }
            soundElement.Source = new Uri(practiceModel.ArraySound[j], UriKind.Relative);
        }

        private void btn_Spelling_Click(object sender, RoutedEventArgs e)
        {
            soundElement.Source = new Uri(practiceModel.ArraySound[n - 1], UriKind.Relative);
        }       
    }
}